//
//  LoginBar.swift
//  FeastBooking
//
//  Created by Imon on 2017/9/21.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit

protocol LoginBarDelegate {
    func showLoginView()
}

class LoginBar: UIView {
    
    var delegate: LoginBarDelegate?
    
    let closeButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "cross"), for: .normal)
        return button
    }()
    
    let descriptiveLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.font = AppFont(size: 14)
        label.text = LanguageHelper.getString(key: "LOGIN_MORE_SERVICE")
        return label
    }()
    
    let loginButton: UIButton = {
        let button = UIButton()
        button.backgroundColor = UIColor.white
        button.layer.cornerRadius = 3
        button.layer.masksToBounds = true
        button.setTitle(LanguageHelper.getString(key: "SIGNIN"), for: .normal)
        button.setTitleColor(buttonColor, for: .normal)
        return button
    }()
    
    let imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        imageView.image = UIImage(named: "background")
        imageView.alpha = 0.75
        return imageView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(closeButton)
        self.addSubview(descriptiveLabel)
        self.addSubview(loginButton)
        self.insertSubview(imageView, at: 0)
        closeButton.addTarget(self, action: #selector(closeView(_:)), for: .touchUpInside)
        loginButton.addTarget(self, action: #selector(loginButtonAction(_:)), for: .touchUpInside)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let cbSize: CGFloat = 16
        let lbSize: CGSize = CGSize(width: 90, height: 30)
        imageView.frame = self.bounds
        closeButton.frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake((self.bounds.height - cbSize) / 2, 15, (self.bounds.height - cbSize) / 2, self.bounds.width - 15 - cbSize))
        descriptiveLabel.frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake((self.bounds.height - cbSize) / 2, 15 + cbSize + 10, (self.bounds.height - cbSize) / 2, 10 + lbSize.width + 15))
        loginButton.frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake((self.bounds.height - lbSize.height) / 2, self.bounds.width - lbSize.width - 10, (self.bounds.height - lbSize.height) / 2, 10))
    }
    
    func closeView(_ sender: UIButton) {
        self.removeFromSuperview()
    }
    
    func loginButtonAction(_ sender: UIButton) {
        delegate?.showLoginView()
    }
}
